import axios from 'axios'

interface OrderItemVO {
  dishId: number
  dishName: string
  price: number
  quantity: number
}
export interface SubmitOrderParam {
  userId: number
  storeId: number
  tableId: number
  items: {
    dishId: number
    quantity: number
    price?: number      // 如果你后端允许不传 price，就 ?
    specId?: number     // 可选，不给就是 undefined
  }[]
}
export interface MyOrderVO {
  id: number
  storeId?: number
  tableId: number
  totalPrice: number
  status: 1 | 2 | 3
  createTime: string
  items: OrderItemVO[]
}
interface ApiResult<T> {
  code: number
  msg: string
  data: T
}

export function submitOrder(data: SubmitOrderParam) {
  return axios.post<ApiResult<number>>('/api/order/submit', data)
}
export const getMyOrders = (userId: number, status?: 1 | 2 | 3) =>
  axios.get<{
    code: number
    msg: string
    data: MyOrderVO[]
  }>('/api/order/my', { params: { userId, status } })
export const getOrderDetail = (id: number) =>
  axios.get<{ code: number; msg: string; data: MyOrderVO }>(`/api/order/${id}`)
export const payOrder = (id: number, channel: string) =>
  axios.post(`/api/order/${id}/pay?channel=${channel}`)
export const cancelOrder = (id: number) => axios.post(`/api/order/${id}/cancel`)